package com.reader.cms.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.magazine.ReaderMagazineColumn;
import com.reader.cms.entity.sys.ReaderContentClassify;
import com.reader.cms.mapper.sys.ReaderContentClassifyMapper;
import com.reader.cms.service.sys.IReaderContentClassifyService;
import com.reader.core.entity.ResultMessageInfo;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gumenghao
 * @description 杂志内容分类
 * @date 2018-10-25
 */
@RestController
@RequestMapping("/api/readercontentclassify")
public class ReaderContentClassifyController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderContentClassifyController.class);

    @Autowired
    public IReaderContentClassifyService iReaderContentClassifyService;

    @Autowired
    public ReaderContentClassifyMapper readerContentClassifyMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param param       其它查询条件，根据情况动态添加
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "param", required = false) String param) {
        Page<ReaderContentClassify> list = iReaderContentClassifyService.selectListPage(currentPage, size, "");
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }


    /**
     * @return
     * @function 查询下拉box所需数据
     */
    @GetMapping("selectBox")
    public ResultMessageInfo selectListPage() {
        List<ReaderContentClassify> list = iReaderContentClassifyService.selectAll();
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }

    /**
     * @param readerContentClassify 实体对象
     * @return
     * @function 添加杂志分类
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody ReaderContentClassify readerContentClassify) {
        ValidatorUtil.validateEntity(readerContentClassify, AddGroup.class);//非空验证
        EntityWrapper<ReaderContentClassify> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("name", readerContentClassify.getName());
        Integer count = readerContentClassifyMapper.selectCount(wrapper);
        if (count != 0) {
            return ResultMessageInfo.ofMessage("内容分类名称已存在！！！");
        }
        Integer userId = this.getUserId();
        readerContentClassify.setInsertDt(new Date());
        readerContentClassify.setDelFlag(0);
        readerContentClassify.setInsertUserId(userId);
        readerContentClassify.setUpdtUserId(userId);
        boolean resultValue = iReaderContentClassifyService.insertOrUpdate(readerContentClassify);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @param readerContentClassify 实体对象
     * @return
     * @function 更新杂志分类数据
     */
    @PostMapping("update")
    @Transactional
    public ResultMessageInfo update(@RequestBody ReaderContentClassify readerContentClassify) {
        ValidatorUtil.validateEntity(readerContentClassify, AddGroup.class);//非空验证
        EntityWrapper<ReaderContentClassify> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("name",
                readerContentClassify.getName()).ne("id", readerContentClassify.getId());
        Integer count = readerContentClassifyMapper.selectCount(wrapper);
        if (count != 0) {
            return ResultMessageInfo.ofMessage("内容分类名称已存在！！！");
        }
        Integer userId = this.getUserId();
        readerContentClassify.setDelFlag(0);
        readerContentClassify.setUpdtDt(new Date());
        readerContentClassify.setUpdtUserId(userId);
        boolean resultValue = iReaderContentClassifyService.updateById(readerContentClassify);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");

        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @return
     * @function 删除单条杂志分类
     */
    @PostMapping("delete")
    @Transactional
    public ResultMessageInfo deleteById(@RequestBody ReaderContentClassify readerContentClassify) {
        ResultMessageInfo resultMessage = null;
        Integer userId = this.getUserId();
        readerContentClassify.setDelFlag(1);
        readerContentClassify.setUpdtDt(new Date());
        readerContentClassify.setUpdtUserId(userId);
        boolean resultValue = iReaderContentClassifyService.updateById(readerContentClassify);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");

    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除杂志分类
     */
    @PostMapping("deleteIds")
    @Transactional
    public ResultMessageInfo deleteBatchIds(@RequestBody String data) {
        ResultMessageInfo resultMessage = null;
        List<Long> ids = JSON.parseArray(JSON.parseObject(data).getString("ids"), Long.class);
        boolean resultValue = iReaderContentClassifyService.deleteBatchIds(ids);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}